{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\data\\2017-07-17_cap_tops.xlsx is saved.\n",
      "[Getting data:]#####"
     ]
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import pandas as pd\n",
    "import os\n",
    "import datetime\n",
    "import numpy as np\n",
    "ts.set_token('ac8805887aa21d24fd5ec47a4ae9e7a09804ebaeedea93352a7b13d2e8f08e46')\n",
    "'''\n",
    "code：代码\n",
    "name:名称\n",
    "count：上榜次数\n",
    "bamount：累积购买额(万)\n",
    "samount：累积卖出额(万)\n",
    "net：净额(万)\n",
    "bcount：买入席位数\n",
    "scount：卖出席位数\n",
    "'''\n",
    "today_now = datetime.date.today()\n",
    "DATA_FILE_PATH = '..\\\\data\\\\'\n",
    "file = DATA_FILE_PATH + str(today_now)+'_cap_tops.xlsx'\n",
    "if os.path.exists(file):\n",
    "    print(file+ \" is exist.\")\n",
    "else:\n",
    "    df = ts.cap_tops()\n",
    "    df.to_excel(file)\n",
    "    print(file + \" is saved.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以从excel文件里面读取\n",
    "# df_cap_tops = pd.read_excel(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\data\\2017-07-17_stock_basics.xlsx is saved.\n"
     ]
    }
   ],
   "source": [
    "file2 = DATA_FILE_PATH + str(today_now)+'_stock_basics.xlsx'\n",
    "if os.path.exists(file2):\n",
    "    print(file2+ \" is exist.\")\n",
    "else:\n",
    "    df = ts.get_stock_basics()\n",
    "    df.to_excel(file2)\n",
    "    print(file2 + \" is saved.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2496</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>code</th>\n",
       "      <td>507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <td>珠海港</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>industry</th>\n",
       "      <td>港口</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>area</th>\n",
       "      <td>广东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pe</th>\n",
       "      <td>140.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>outstanding</th>\n",
       "      <td>7.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>totals</th>\n",
       "      <td>7.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>totalAssets</th>\n",
       "      <td>577226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>liquidAssets</th>\n",
       "      <td>93139.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fixedAssets</th>\n",
       "      <td>252498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>reserved</th>\n",
       "      <td>78896.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>reservedPerShare</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>esp</th>\n",
       "      <td>0.019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bvps</th>\n",
       "      <td>3.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pb</th>\n",
       "      <td>3.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>timeToMarket</th>\n",
       "      <td>19930326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>undp</th>\n",
       "      <td>91257.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>perundp</th>\n",
       "      <td>1.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rev</th>\n",
       "      <td>10.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>profit</th>\n",
       "      <td>-38.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>gpr</th>\n",
       "      <td>23.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>npr</th>\n",
       "      <td>3.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>holders</th>\n",
       "      <td>135569</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      2496\n",
       "code                   507\n",
       "name                   珠海港\n",
       "industry                港口\n",
       "area                    广东\n",
       "pe                   140.7\n",
       "outstanding           7.72\n",
       "totals                 7.9\n",
       "totalAssets         577226\n",
       "liquidAssets       93139.3\n",
       "fixedAssets         252498\n",
       "reserved           78896.8\n",
       "reservedPerShare         1\n",
       "esp                  0.019\n",
       "bvps                  3.33\n",
       "pb                    3.26\n",
       "timeToMarket      19930326\n",
       "undp               91257.1\n",
       "perundp               1.16\n",
       "rev                  10.14\n",
       "profit              -38.97\n",
       "gpr                  23.96\n",
       "npr                   3.88\n",
       "holders             135569"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stock_basics = pd.read_excel(file2)\n",
    "'''\n",
    "基本面分析包括下面的参数：\n",
    "code,代码\n",
    "name,名称\n",
    "industry,所属行业\n",
    "area,地区\n",
    "pe,市盈率\n",
    "outstanding,流通股本(亿)\n",
    "totals,总股本(亿)\n",
    "totalAssets,总资产(万)\n",
    "liquidAssets,流动资产\n",
    "fixedAssets,固定资产\n",
    "reserved,公积金\n",
    "reservedPerShare,每股公积金\n",
    "esp,每股收益\n",
    "bvps,每股净资\n",
    "pb,市净率\n",
    "timeToMarket,上市日期\n",
    "undp,未分利润\n",
    "perundp, 每股未分配\n",
    "rev,收入同比(%)\n",
    "profit,利润同比(%)\n",
    "gpr,毛利率(%)\n",
    "npr,净利润率(%)\n",
    "holders,股东人数\n",
    "'''\n",
    "# 这里的code不是字符了，注意\n",
    "df_stock_basics[(df_stock_basics['code']==507)].T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
